<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 数组扁平化
        console.log([1,[2,[3,[4,5]]]].flat(3));
        // 递归
        function flatten1(arr){
            let res = [];
            arr.forEach(item => {
                if(Array.isArray(item)){
                    res = res.concat(flatten1(item));
                }else{
                    res.push(item);
                }
            })
            return res;
        }
        console.log(flatten1([1,[2,[3,[4,5]]]]));
        // reduce
        // function flatten2(arr){
        //     return arr.reduce((pre,cur) => {
        //         return pre.concat(Array.isArray(cur) ? flatten2(cur) : cur);
        //     })
        // }
        // console.log(flatten2([1,[2,[3,[4,5]]]]));
        // toString
        function flatten3(arr){
            return arr.toString().split(',').map(item => Number(item));
        }
        console.log(flatten3([1,[2,[3,[4,5]]]]));
        // 扩展运算符
        function flatten4(arr){
            while(arr.some(item => Array.isArray(item))){
                arr = [].concat(...arr);
            }
        }
        console.log(flatten4([1,[2,[3,[4,5]]]]));
    </script>
</body>
</html>